package com.softeem.ssm2;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @Author mrchai
 * @Date 2019/10/12 16:40
 */
//@ComponentScans({
//        @ComponentScan("com.softeem.dao"),
//        @ComponentScan("com.softeem.service"),
//        @ComponentScan("com.softeem.controller")
//})
@EnableAspectJAutoProxy
@ComponentScan("com.softeem.ssm2")
@MapperScan("com.softeem.ssm2.dao")
@EnableTransactionManagement
@PropertySource("classpath:jdbc.properties")
@Configuration
public class AppContextConfig {

    @Value("${jdbc.driver}")
    private String driverClassName;

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.username}")
    private String username;

    @Value("${jdbc.password}")
    private String password;

    @Value("${jdbc.maxActive}")
    private int maxActive;

    @Value("${jdbc.maxWait}")
    private long maxWait;

    /**
     * 数据源配置
     * @return
     */
    @Bean
    public DruidDataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setMaxActive(maxActive);
        dataSource.setMaxWait(maxWait);
        return dataSource;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource());
        bean.setTypeAliasesPackage("com.softeem.ssm2.entity");

        //获取路径匹配解析器（读取映射文件）
        PathMatchingResourcePatternResolver pm = new PathMatchingResourcePatternResolver();
        //将映射文件读取为Resource数组
        Resource[] resources = pm.getResources("classpath:mappers/*.xml");
        bean.setMapperLocations(resources);

        //创建配置并设置
        org.apache.ibatis.session.Configuration cfg = new org.apache.ibatis.session.Configuration();
        cfg.setCacheEnabled(true);
        cfg.setLazyLoadingEnabled(false);
        cfg.setUseGeneratedKeys(true);
        bean.setConfiguration(cfg);

        //配置插件
        bean.setPlugins(new Interceptor[]{new PageInterceptor()});

        return bean.getObject();
    }

    /**
     * 事务管理器
     * @return
     */
    @Bean
    public DataSourceTransactionManager transactionManager(){
        return new DataSourceTransactionManager(dataSource());
    }

}
